import Vue from 'vue'
import VueRouter from 'vue-router'
import { getToken } from '@/utils/token'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/layout'
  },
  {
    path: '/login',
    component: () => import(/* webpackChunkName: "Login" */ '@/views/Login')
  },
  {
    path: '/layout',
    component: () => import(/* webpackChunkName: "Layout" */'@/views/Layout'),
    redirect: '/layout/home',
    children: [
      {
        path: 'home',
        component: () => import(/* webpackChunkName: "Home" */ '@/views/Home')
      },
      {
        path: 'user',
        component: () => import(/* webpackChunkName: "User" */ '@/views/User')
      }
    ]
  },
  {
    // 编辑个人信息
    path: '/user_edit',
    component: () => import('@/views/User/UserEdit')
  },
  {
    // 搜索页面
    path: '/search',
    component: () => import('@/views/Search')
  },
  {
    path: '/search-result',
    component: () => import('@/views/Search/SearchResult')
  },
  {
    path: '/article-detail',
    component: () => import('@/views/ArticleDetail')
  },
  {
    path: '/chat',
    component: () => import('@/views/Chat')
  },
  {
    path: '/reply',
    component: () => import('@/views/ArticleDetail/Reply')

  }
]

const router = new VueRouter({
  routes
})

// 前置路由守卫
router.beforeEach((to, from, next) => {
  const token = getToken('token')
  if (token === null && to.path !== '/login') {
    next('/login') // 强制前往‘/login’
  } else {
    next()
  }
})
export default router
